package com.itheima.reggie.controller;

import com.itheima.reggie.common.Constant;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.vo.ResultInfo;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeSerivce;
   /* @Autowired
    HttpSession session;*/

    @PostMapping("/employee/login")
    public ResultInfo login(@RequestBody Object p2,HttpSession session) {
        System.out.println(p2);
        Map<String,String> p= (Map<String, String>) p2;
        String username = p.get("username");
        String password = p.get("password");
        ResultInfo resultInfo = employeeSerivce.login(username, password);
        if (resultInfo.getCode() == 1) {
            Object employee = resultInfo.getData();
            session.setAttribute(Constant.SESSION_EMPLOYEE, employee);
        }
        return resultInfo;


    }

    @PostMapping("/employee/logout")
    public ResultInfo logout(HttpSession session)
    {session.invalidate();
    return ResultInfo.success(null);

    }

    @GetMapping("/employee/find")
    public ResultInfo findList(String name)
    {
      List<Employee> list=  employeeSerivce.findList(name);
      return ResultInfo.success(list);
    }
    @GetMapping("/employee/{id}")
    public ResultInfo findById(@PathVariable("id") Long id)
    {Employee employee= employeeSerivce.findById(id);
    return ResultInfo.success(employee);

    }
    //走添加方法
    @PostMapping("/employee")
    public  ResultInfo save(@RequestBody Employee emp)
    {
        employeeSerivce.add(emp);
        return ResultInfo.success(null);
    }
    //走修改方法
    @PutMapping("/employee")
    public ResultInfo edit(@RequestBody Employee emp)
    {
        employeeSerivce.update(emp);

        return ResultInfo.success(null);
    }
}
